package com.czk.templatemessage;

import com.alibaba.fastjson.JSONObject;
import com.czk.pojo.templateMessagepojo.First;
import com.czk.pojo.templateMessagepojo.Remark;
import com.czk.pojo.templateMessagepojo.location.LocationTemplateMessage;
import com.czk.pojo.templateMessagepojo.location.PersonLocation;
import com.czk.pojo.templateMessagepojo.resume.*;
import com.czk.pojo.templateMessagepojo.vip.*;
import com.czk.utlls.TokenUtil;
import com.czk.utlls.WeChatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/8/3 11:33
 */
@Component
public class TemplateMessageOperate {
    @Value("${industry:www.baidu.com}")
    private String url;
    @Value("${getIndustry:www.baidu.com}")
    private String getIndustrtUrl;
    @Value("${templateId:www.baidu.com}")
    private String templateId;
    @Value("${templateMessUrl:http://www.baidu.com}")
    private String templateMessUrl;
    @Autowired
    private TokenUtil tokenUtil;

    public String setIndustry() {
        // 拿到token
        String accessToken = tokenUtil.getToken().getAccessToken();
        //
        url = url.replace("ACCESS_TOKEN", accessToken);
        // 设置行业
        String data = "{\n" +
                "    \"industry_id1\":\"1\",\n" +
                "    \"industry_id2\":\"4\"\n" +
                "}";
        // 发送post请求
        String result = WeChatUtil.post(url, data);
        System.out.println(result);
        return result;
    }

    /**
     * 获取行业信息
     *
     * @return
     */
    public String getIndustry() {
        // 拿到token
        String accessToken = tokenUtil.getToken().getAccessToken();
        getIndustrtUrl = getIndustrtUrl.replace("ACCESS_TOKEN", accessToken);
        try {
            URL url = new URL(getIndustrtUrl);
            URLConnection urlConnection = url.openConnection();
            // 拿到输出流
            InputStream inputStream = urlConnection.getInputStream();
            byte[] b = new byte[1024];
            int length = 0;
            StringBuilder builder = new StringBuilder();
            while ((length = inputStream.read(b)) != -1) {
                builder.append(new String(b, 0, length));
            }
            String str = builder.toString();
            System.out.println(str);
            return str;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String sendTemplateMessage() {
        // 拿到token
        String accessToken = tokenUtil.getToken().getAccessToken();
        // 替换
        templateMessUrl = templateMessUrl.replace("ACCESS_TOKEN", accessToken);
        ResumeTemplateMessage resumeTemplateMessage = new ResumeTemplateMessage();
        // 设置userid
        resumeTemplateMessage.setTouser("o365s57rVcua532rJ9zD2US518Wk");
        // 设置模板id
        resumeTemplateMessage.setTemplate_id(templateId);
        // 设置Data
        Data data = new Data();
        // 根据模板id的参数来设置
        data.setFirst(new First("您有新的反馈信息拉", "#17177"));
        data.setCompany(new Company("陈彰坤的公司", "#fff000"));
        data.setTime(new Time("2021/08/03", "#1f1f1f"));
        data.setResult(new Result("面试通过", "#1f1f1f"));
        data.setRemark(new Remark("清和公司认识专员联系", "#17177"));
        resumeTemplateMessage.setData(data);
        // 咋混为json
        String jsonString = JSONObject.toJSONString(resumeTemplateMessage);
        System.out.println(jsonString);
        String s = WeChatUtil.post(templateMessUrl, jsonString);
        return s;
    }

    /**
     * 发送会员卡变更的模板消息
     *
     * @return
     */
    public String sendVipTemplateMessage() {
        String templateId = "45ekMzrSnNZPxTteky8kEfBjzqkaJLtvyHQYxHKdFPs";
        // 拿到token
        String accessToken = tokenUtil.getToken().getAccessToken();
        templateMessUrl = templateMessUrl.replace("ACCESS_TOKEN", accessToken);
        // 替换
        VipTemplateMessage vipTemplateMessage = new VipTemplateMessage();
        vipTemplateMessage.setTemplate_id(templateId);
        vipTemplateMessage.setTouser("o365s57rVcua532rJ9zD2US518Wk");
        vipTemplateMessage.setUrl("http://wwww.baidu.com");
        VipCard vipCard = new VipCard();
        vipCard.setFirst(new First("您的资料已经成功变更", "#17177"));
        vipCard.setCarNum(new CarNum("1052031630", "fff000"));
        vipCard.setContent(new Content("升级为super vip", "#1f1f1f"));
        vipCard.setName(new Name("陈阿坤先生", "#17175"));
        vipCard.setRemark(new Remark("谢谢升级", "#15452"));
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        String format = dateTimeFormatter.format(LocalDateTime.now());
        vipCard.setTime(new Time(format, "#17175"));
        vipTemplateMessage.setVipCard(vipCard);
        String string = JSONObject.toJSONString(vipTemplateMessage);
        // 官方要求必须为data
        string = string.replace("vipCard", "data");
        System.out.println(string);
        String post = WeChatUtil.post(templateMessUrl, string);
        return post;
    }

    /**
     * 发送地理位置信息的模板消息
     *
     * @param personLocation
     * @return
     */
    public String sendLocationTemplateMessage(PersonLocation personLocation,String userId) {
        String templateId = "i12Xjmsd7xtfx_BGq2Qf-ecFZDLzFkYEgtmqjAAEAd8";
        // 拿到token
        String accessToken = tokenUtil.getToken().getAccessToken();
        templateMessUrl = templateMessUrl.replace("ACCESS_TOKEN", accessToken);
        // 设置人
        LocationTemplateMessage locationTemplateMessage = new LocationTemplateMessage();
        locationTemplateMessage.setTemplate_id(templateId);
        locationTemplateMessage.setTouser(userId);
        // 设置内容
        locationTemplateMessage.setPersonLocation(personLocation);
        // 转为json
        String result = JSONObject.toJSONString(locationTemplateMessage);
        // 替换
        result = result.replace("personLocation", "data");
        System.out.println(result);
        String post = WeChatUtil.post(templateMessUrl, result);
        return post;
    }
}
